﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Collections;
using System.Data;

public partial class Management_AddInformation : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string gid = TextBox1.Text;
        string id = TextBox2.Text;
        string name = TextBox3.Text;
        string gpa = TextBox4.Text;
        string score = TextBox5.Text;
        string introduction = TextBox6.Text;
        string department = TextBox8.Text;
        if (gid == "1" || gid == "3")
        {
            DBaccess db = new DBaccess();
            SqlDataReader read = db.GetReader("select count(*) from mr_member where mid ='" + TextBox2.Text + "'");
            read.Read();
            if (int.Parse(read.GetValue(0).ToString()) > 0)
                Response.Write("<script>alert('该账号已存在!');</script>");
            else
            {
                string sql = "insert into mr_member(mid,mname,gid,password) values('" + id + "','" + name + "','" + gid + "','111')";//数据库操作
                string sql1 = "insert into mr_teacher(tid,introduction) values ('" + id + "','" + introduction + "')";
                if (db.ExecSql(sql) != 0 && db.ExecSql(sql1) !=0)
                {
                    Response.Write("<script>alert('添加成功!');</script>");
                    TextBox1.Text = "";
                    TextBox2.Text = "";
                }
                else
                {
                    Response.Write("<script>alert('添加出错!');</script>");
                }
            }
        }
        else if (gid == "2")
        {
            DBaccess db = new DBaccess();
            SqlDataReader read = db.GetReader("select count(*) from mr_member where mid ='" + TextBox2.Text + "'");
            read.Read();
            if(int.Parse(read.GetValue(0).ToString()) >0)
                Response.Write("<script>alert('该账号已存在!');</script>");
            else
            {
                string sql = "insert into mr_member(mid,mname,gid,password) values('" + id + "','" + name + "','" + gid + "','111')";//数据库操作
                string sql1 = "insert into mr_student(sid,gpa,score,department) values('" + id + "','" + gpa + "','" + score + "','" + department + "')";
                if (db.ExecSql(sql) != 0 && db.ExecSql(sql1)!=0)
                {
                    Response.Write("<script>alert('添加成功!');</script>");
                    TextBox1.Text = "";
                    TextBox2.Text = "";
                }
                else
                {
                    Response.Write("<script>alert('添加出错!');</script>");
                }
            }
        }
        else
            Response.Write("<script>alert('输入错误!');</script>");
        
    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        string gid = TextBox1.Text;
        if (gid == "1" || gid == "3")
        {
            TextBox4.Enabled = false;
            TextBox5.Enabled = false;
            Label6.Enabled = false;
            Label7.Enabled = false;
            Label1.Enabled = false;
            TextBox8.Enabled = false;
        }
        else if (gid == "2")
        {
            Label8.Enabled = false;
            TextBox6.Enabled = false;
        }
        else
            Response.Write("<script>alert('输入错误!');</script>");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Session["search"] = TextBox7.Text;
        Response.Redirect("./Search.aspx");
    }
    
}